import SubTitle from '@/pages/OrderModule/common/components/SubTitle';
import Typography from '@/pages/OrderModule/common/components/Typography';
import { Radio, Space } from 'antd';
import { useState, useEffect, useRef, useMemo } from 'react';
import orderUtils from '@/pages/OrderModule/common/utils/order';
import { getOrderIdsOp } from '../../../services';
import { cloneDeep } from 'lodash';

/**
 * 订单范围 单选框
 * @param {object} props
 * @param {object} props.wrapStyle 外层样式
 */
function OrderAreaRadioGroup(props) {
  const { wrapStyle = {}, orderList, searchForm, selectKeys, states = [], otherRulesFunc, updateShowNumer, title = '仅限待审核、异常状态的自发订单', ...args } = props || {};
  
  const [type, setType] = useState('a');
  const [loading, setLoading] = useState(false);

  const stageParams = useRef({});

  // const isSearch = useMemo(()=>{
  //   const params = cloneDeep(searchForm);
  //   const { orderTypeMap } = params;
  //   if(orderTypeMap && typeof orderTypeMap == 'object'){
  //     const values = Object.values(orderTypeMap);
  //     if(values.filter(item=>item)?.length){
  //       return true;
  //     }
  //   }
  //   delete params.orderTypeMap;
  //   const arr = Object.keys(params), len = arr.length;
  //   for(let i = 0; i < len; i++){
  //     const currentValue = params[arr[i]], dataType = typeof currentValue;
  //     if(currentValue){//真值
  //       if(Array.isArray(currentValue) && !currentValue?.length){
  //         delete params[arr[i]];
  //       }
  //     }else if(['string', 'undefined', 'object'].includes(dataType)){//假值
  //       delete params[arr[i]];
  //     }
  //   }
    
  //   console.log(searchForm, params)
  //   delete params.pageNum;
  //   delete params.pageSize;
  //   delete params.timeType;
  //   delete params.sortBy;
  //   delete params.isOrderTypeOr;
  //   delete params.sortType;
  //   return Boolean(Object.keys(params)?.length);
  // }, [searchForm])

  useEffect(()=>{
    if(type == 'a'){
      if(!stageParams.current.numObj1){
        const sourceData = orderUtils.commonFilterOrder({
          states,
          otherRulesFunc,
          sourceData: orderUtils.getSelectedRowData(selectKeys, orderList),
        }),
        numObj = orderUtils.statisticsNumbers(states, sourceData);
        stageParams.current.numObj1 = numObj;
      }
       updateShowNumer?.(stageParams.current.numObj1, selectKeys);
    }else{
      getOrderIds();
    }
  }, [type])

  //临时兼容已经铺设联调的部分
  const formatNumberObj = (numberObj) => {
    const fieldsMap = {
      auditCount: 10,
      shippingCount: 20,
      exceptionCount: 30,
      shippedCount: 40,
      cancelledCount: 50,
      totalCount: 'total'
    }
    const arr = Object.keys(numberObj);
    arr.map(item=>{
      numberObj[fieldsMap[item]] = numberObj[item];
    })
    // console.log(numberObj)
    return numberObj;
  }

  const getOrderIds = async () => {
    let orderIds = stageParams.current.orderIds || [];
    if(!stageParams.current.numObj2){
      setLoading(true);
      const { data, success} = await getOrderIdsOp(searchForm);
      setLoading(false);
      if(success && data?.orderList?.length){
        const { orderList, ...numberObj} = data || {};
        orderIds = orderList;
        stageParams.current.numObj2 = formatNumberObj(numberObj);
        stageParams.current.orderIds = orderList;
      }
    }
    updateShowNumer?.(stageParams.current.numObj2, orderIds);
  }
  
  return (
    <div style={wrapStyle}>
      <SubTitle symbol as="s2" title="订单范围" wrapStyle={{ marginBottom: 16 }} />
      <Radio.Group {...args} value={type} onChange={e=>setType(e.target.value)}>
        <Space direction="vertical">
          <Radio value="a">订单列表勾选的订单</Radio>
          <Radio value="b">
            <div>
              所有符合条件的订单<Typography color="#666">（加入搜索条件过滤后）</Typography>
              {/* { !isSearch && <Typography color="#F03D29">您还未搜索订单</Typography>} */}
            </div>
            <Typography color="#999">{title}</Typography>
          </Radio>
        </Space>
      </Radio.Group>
    </div>
  );
}
export default OrderAreaRadioGroup;
